#include "relay.h"

/**
* @brief Relay initialization
* @param
*    
*    
* @return none
*/
void relay_pin_config(void)
{
    //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);    //时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);    //时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    //时钟
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    RELAY0_OFF;
    RELAY1_OFF;
    RELAY2_OFF;
    RELAY3_OFF;
    RELAY4_OFF;
    RELAY5_OFF;
}


/**
* @brief Relay Control
* @param
*    
*    
* @return none
*/
void relay_control(void)
{
    u8 i=0; 
    u32 start[6]={0};      //开始时间s
    u32 over[6]={0};       //结束时间s
    u32 now_time=0;             //当前时间s
    
    now_time=(u32)Hour*3600+(u32)Minute*60+Second;    //时间转换为s
    if(now_time >0 && sys_mode ==0) //已经读取到当前时间并且定时模式
    {
        for(i=0;i<6;i++)
        {
            start[i]=(u32)relay.start_time[i][3]*3600+(u32)relay.start_time[i][4]*60+relay.start_time[i][5];      //时分秒转换为s
            over[i]=(u32)relay.over_time[i][3]*3600+(u32)relay.over_time[i][4]*60+relay.over_time[i][5];          //时分秒转换为s
            
            if(start[i] < over[i])  //开始时间小于结束时间
            {
                if(now_time >= over[i])
                { 
                    relay.relay_switch[i]=0;   //关闭继电器
                }
                else
                {
                    if(now_time >= start[i])
                    {
                        relay.relay_switch[i]=1;   //开继电器
                    }
                }
            }
            else if(start[i] > over[i])  //开始时间大于结束时间,则第二天关闭
            {
                if(now_time >= start[i])
                {
                    relay.relay_switch[i]=1;   //开继电器
                }
                else if(now_time >= over[i])
                {
                     relay.relay_switch[i]=0;   //关闭继电器
                }
                
            }
            else   //开始时间等于结束时间永久打开
            {
                if(start[i]!=0 || over[i]!=0)
                relay.relay_switch[i]=1;   //开继电器
            }
        }
    }
    //每天开启和关闭时间,不需要判断日期
    for(i=0;i<6;i++)
    {
        if(relay.relay_switch[i])      //开启继电器
        {
            switch(i)
            {
                case 0:RELAY0_ON;break;
                case 1:RELAY1_ON;break;
                case 2:RELAY2_ON;break;
                case 3:RELAY3_ON;break;
                case 4:RELAY4_ON;break;
                case 5:RELAY5_ON;break;
            }
        }
        else                           //关闭继电器
        {
            switch(i)
            {
                case 0:RELAY0_OFF;break;
                case 1:RELAY1_OFF;break;
                case 2:RELAY2_OFF;break;
                case 3:RELAY3_OFF;break;
                case 4:RELAY4_OFF;break;
                case 5:RELAY5_OFF;break;
            }
        }
    }
}























